package com.obas.business.component.tree.treeclass {

  import mx.collections.ArrayCollection;

   /**
    * For create different types of node.
    */
  public class LoadingTreeNodeFactory {

      public static const NODE_ELEMENT:String = "node";
      public static const NODE_ELEMENTTYPE:String = "type";
      public static const NODE_TYPE:String = "nodetype";

      // Define node types.
      public static const NODETYPE_EXPLORER:String = "explorer";
      public static const NODETYPE_ENTITY:String = "entity";
      public static const NODETYPE_LEAF:String = "leaf";

      private var hasInitialized:Boolean = false;

      // Assort all nodes by node type.
      private var _inventoryCollection:ArrayCollection = new ArrayCollection();
      private var _aggregateCollection:ArrayCollection = new ArrayCollection();
      private var _leafCollection:ArrayCollection = new ArrayCollection();

      public function get inventoryCollection():ArrayCollection {
         if (!hasInitialized) {
            if (initNodeTypeCollections()) {
               return _inventoryCollection;
            }
         }
         return _inventoryCollection;
      }

      public function get aggregateCollection():ArrayCollection {
         if (!hasInitialized) {
            if (initNodeTypeCollections()) {
               return _aggregateCollection;
            }
         }
         return _aggregateCollection;
      }

      public function get leafCollection():ArrayCollection {
         if (!hasInitialized) {
            if (initNodeTypeCollections()) {
               return _leafCollection;
            }
         }
         return _leafCollection;
      }

      /**
      * After loading the configuration file, should initialize the arrayCollection sort by node type.
      */
      public function initNodeTypeCollections():Boolean {
         hasInitialized = true;
         return true;
      }

      public function createNode(item:Object, labelField:String = null):Node {
         var node:Node = new Node();
         if (!labelField) {
            node.labelField = "name";
         }
         else {
            node.labelField = labelField;
         }
         node.entity = item;
         node.children = new ArrayCollection();
         return node;
      }

      /**
      * Create LoadingNode.
      */
      public function createLoadingTreeNode():LoadingNode {
         return new LoadingNode();
      }

      /**
      * Create ErrorNode.
      */
      public function createErrorTreeNode(errorMsg:String):ErrorNode {
         return new ErrorNode(errorMsg);
      }
  }
}
